多表关联同时更新多条不同的记录方法分享
1004 redo size
1 d 四
840 bytes sent via SQL*Net to client
----------------------------------------------------------------------------
id number,
SQL> update (select ta.b tab1 ,tb.b tbb from ta,tb where ta.a=tb.a) set tab1=tbb;
=================
0 physical reads
chi varchar2(3),
8 recursive calls
0 sorts (disk)
11 recursive calls
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
0 physical reads
testA:
----------------------------------------------------------
复制代码 代码如下:
3 sorts (memory)标题可能没说清楚,假设有这样两张表:
3 SQL*Net roundtrips to/from client
age int
From #temptest1 t1
| 3 | TABLE ACCESS FULL| TA | 5 | 100 | 2 (0)| 00:00:01 |1 sorts (memory)
create table testB(
)
SQL> update ta set ta.b=(select tb.b from tb where ta.a=tb.a) where exists (select 1 from tb where ta.a=tb.a);
|* 6 | TABLE ACCESS FULL | TB | 1 | 26 | 2 (0)| 00:00:01 |
(
eng varchar2(3),
840 bytes sent via SQL*Net to client
testB:
2 - access("TA"."A"="TB"."A")
| 1 | UPDATE | TA | | | | |
| 0 | UPDATE STATEMENT | | 5 | 165 | 20 (30)| 00:00:01 |
| 5 | TABLE ACCESS FULL| TB | 4 | 52 | 2 (0)| 00:00:01 |
Predicate Information (identified by operation id):
已用时间: 00: 00: 00.00
|* 4 | TABLE ACCESS FULL| TB | 1 | 13 | 2 (0)| 00:00:01 |
2 - access("TA"."A"="ITEM_1")
)
0 sorts (disk)
实现方法如下:
2 - filter("TA"."A"= (SELECT "TB"."A" FROM "TB" "TB" WHEREage int
53 consistent gets
--------------------------------------------------------------------------------
3 c 三
执行计划
----------------------------------------------------------------------------
---------------------------------------------------
1004 redo size
1 sorts (memory)
复制代码 代码如下:
17 consistent gets|* 2 | FILTER | | | | | |
ID ENG CHI
create table testA(
)
4 db block gets
当然这里的要求是只用一句SQL,不能用循环。
Plan hash value: 1137212925CODE:
588 redo size
已更新4行。
| 1 | UPDATE | TA | | | | |
name1 varchar(50),
827 bytes received via SQL*Net from client
--------------------------------------------------------------------------------
我想把testB中的记录的ENG,CHI字段更新到testA中去,以ID来对应。
如果 create unique index tb_a_uidx on tb(a);
----------------------------------------------------------
[Copy to clipboard] [ - ]
----------------------------------------------------------------------------
ID ENG CHI ANY....
-----
Note
- dynamic sampling used for this statement (level=2)
update set t1.age = t2.age----------------------------------------------------------
已更新4行。
858 bytes received via SQL*Net from client
6 - filter("TB"."A"=:B1)
(补充)Sql Server 2008 Merge命令写法:
---------------------------------------------------merge into #temptest1 t1
id int,
如何一次性更新多条不同值的记录
3 SQL*Net roundtrips to/from client
统计信息
anythingother varchar2(1)
现有记录
----------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
)
结果如下:
是不是挺有趣的Sql。
3 f 六
----------------------------------------------------------
| 4 | TABLE ACCESS FULL| TA | 5 | 100 | 2 (0)| 00:00:01 |
5 - filter("TB"."A"=:B1)
23 consistent gets
SQL> update ta set ta.b=(select tb.b from tb where ta.a=tb.a) where ta.a= (select tb.a from tb where ta.a=tb.a);
|* 2 | HASH JOIN | | 4 | 184 | 5 (20)| 00:00:01 |
统计信息
- dynamic sampling used for this statement (level=2)
执行计划
3 SQL*Net roundtrips to/from client
4 rows processed
以下为测试例子。
using(select age,id from #temptest2) t2
---------------------------------------------------
===============
(
统计信息
----------------------------------------------------------
Predicate Information (identified by operation id):
0 recursive calls
Plan hash value: 3571861550
4 rows processed
0 sorts (disk)
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
id int,
| 4 | VIEW | VW_SQ_1 | 4 | 52 | 2 (0)| 00:00:01 |
2 e 五
id number,
| 3 | TABLE ACCESS FULL| TB | 4 | 104 | 2 (0)| 00:00:01 |
856 bytes received via SQL*Net from client
| 3 | TABLE ACCESS FULL | TA | 5 | 100 | 2 (0)| 00:00:01 |
On t1.id = t2.id
2.现在要将#temptest2中的年龄更新到相应的#temptest1中的年龄。
Note| 0 | UPDATE STATEMENT | | 1 | 20 | 7 (15)| 00:00:01 |
--------------------------------------------------------------------------------
1.首先创建两张临时表并录入测试数据:
-----
create table #temptest1
1 db block gets
|* 2 | HASH JOIN SEMI | | 5 | 165 | 5 (20)| 00:00:01 |
Set t1 .age = t2.age
Note已用时间: 00: 00: 00.01
4 db block gets
其实就是让[表1]中ID为1的年龄改成19,同时ID为2的年龄改成20。
SQL> set autot on
chi varchar2(3)
0 physical reads
Join #temptest2 t2
查询出此时的表数据为:
4 - filter("TB"."A"=:B1)|* 5 | TABLE ACCESS FULL | TB | 1 | 26 | 2 (0)| 00:00:01 |
----------------------------------------------------------------------------
eng varchar2(3),
-----
Update t1
840 bytes sent via SQL*Net to client1 a 一
| 1 | UPDATE | TA | | | | |
CODE:
4 rows processed
执行计划
| 0 | UPDATE STATEMENT | | 4 | 184 | 5 (20)| 00:00:01 |
Predicate Information (identified by operation id):
#temptest1 #temptest2
2 b 二已更新4行。
create table #temptest2
已用时间: 00: 00: 00.01
name1 varchar(50),
- dynamic sampling used for this statement (level=2)
"TB"."A"=:B1))
----------------------------------------------------------------------------
when matched then
on t1.id = t2.id
相关热词: 方法
本站内容来源于网络,如有侵权请与我们联系,我们会及时删除,我们深感抱歉!
注:本站所有信息仅供用于网络技术学习参考,学习中请遵循相关法律法规!
本文地址: https://v30.fanwenzhu.com/sql/mssql/1157.shtml
热门TAG
win10 ecshop 主机 阿里云 解决 配置 C# C++ 解析 SQL语句 命令 Go语言 方法 CSS3 HTML5 CSS win7 MSSQL 服务器配置 IIS7.5 IIS7 IIS6 IIS CentOS 7 Linux oracle数据库 oracle phpcms discuz discuz教程最新文章
-
SQL基本教程之行转列Pivo
时间:2021-01-20
-
region from hr.Employees union
时间:2021-01-20
-
有时候需要调整用户权限
时间:2021-01-19
-
(但使用 ORDER BY 子句并不
时间:2021-01-19
-
RAND()*10000)insert into Detail
时间:2021-01-19
-
OR 运算符:在两侧的查询
时间:2021-01-19
-
放假之前老大跟我提起了
时间:2021-01-19
-
数据库的运维计策剧本篇
时间:2021-01-19
热门文章
-
4.与聚合函数和 GROUP BY 子句有关的常见错
时间:2021-01-19
-
SQL Server安全(11/11):审核(Auditing)
时间:2021-01-09
-
sqlserver中查询横表变竖表的sql语句简析
时间:2020-12-08
-
SQL Server简单模式下误删除堆表记录恢复方
时间:2020-12-12
-
关于SQL Server查询语句的使用
时间:2020-12-13
-
MSSQL教程_mssql数据库教程_MSSQL基础教程_第
时间:2020-12-13
-
jdbc连接sql server数据库问题分析
时间:2020-12-10
-
详解SQL游标的用法
时间:2020-12-27
-
sql server 关于设置null的一些建议
时间:2020-12-28
-
mssql关于一个表格结构的另外一种显示(表
时间:2020-12-11
